<?php
App::uses('AppController', 'Controller');
/**
 * Articles Controller
 *
 * @property Article $Article
 * @property PaginatorComponent $Paginator
 */
class ArticlesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator','Cookie','Session');
	public $paginate = array(
        'conditions' => array('article_category_id' => 1),
        'limit' => 5,
        'order' => array(
            'Article.created_date' => 'DESC'
        )
    );
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Article->recursive = -1;
        $this->Paginator->settings = $this->paginate;
		$this->set('articles', $this->Paginator->paginate());
        
        $this->Article->recursive = -1;
        $this->set('recentArticle', $this->Article->find('all', array('conditions' => array('article_category_id' => 1), 'order' => 'created_date DESC', 'limit' => 6, 'offset' => 5 )));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Article->exists($id)) {
			throw new NotFoundException(__('Invalid article'));
		}
		$options = array('conditions' => array('Article.' . $this->Article->primaryKey => $id));
		$this->set('article', $this->Article->find('first', $options));
        
        $this->Article->recursive = -1;
        $this->set('otherArticle', $this->Article->find('all', array('conditions'=>array('article_id <>' => $id, 'article_category_id' => 1), 'order' => 'created_date DESC', 'limit' => 10 )));
	}

    public function search($date = null) {
        $start = $date . '-1';
        $end = $date . '-30';
		$this->Article->recursive = -1;
        $this->set('articles', $this->Article->find('all', array('conditions'=>array('created_date >=' => $start, 'created_date <=' => $end, 'article_category_id' => 1), 'limit' => 20 )));
        
        $this->Article->recursive = -1;
        $this->set('recentArticle', $this->Article->find('all', array('conditions' => array('article_category_id' => 1),'order' => 'created_date DESC', 'limit' => 6, 'offset' => 6 )));
	}

	public function updateLanguage() {
        $this->autoRender = false;
        $this->Session->write('Config.language', $this->request->data('lang'));
        exit;
    }
}
